package com.example.mysql.subscribe.listener;

import com.example.mysql.debezium.framework.TableListener;
import com.github.shyiko.mysql.binlog.event.EventType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Date;

@Configuration
public class BinlogListenerConfig {

    @Component
    public static class StudentBinlogListener extends TableListener.Base {
        private final Logger log = LoggerFactory.getLogger(StudentBinlogListener.class);

        public StudentBinlogListener() {
            super("demo-test", "t_student");
        }

        @Override
        protected void onUpdateRow(EventType eventType, Serializable[] row) {
            log.info("eventType: {}, data: {}", eventType, row);
            log.info("id: {}", (Integer) row[0]);
            log.info("name: {}", new String((byte[]) row[1]));
            log.info("sex: {}", (Integer) row[2]);
            log.info("category: {}", new String((byte[]) row[3]));
            log.info("create_time: {}", new Date((Long) row[4]));
            log.info("status: {}", (Integer) row[5]);
            System.out.println(row[0]);
        }

        @Override
        protected void onDeleteRow(EventType eventType, Serializable[] row) {
            log.info("eventType: {}, data: {}", eventType, row);
            System.out.println(row[0]);
        }
    }


    @Component
    public static class UserBinlogListener extends TableListener.Base {
        private final Logger log = LoggerFactory.getLogger(UserBinlogListener.class);

        public UserBinlogListener() {
            super("demo-test", "t_user");
        }

    }
}
